import java.io.*;
import java.util.Scanner;
import java.util.Date;

public class cmd {

	public static void main(String[] args) {
       System.out.println("CMD");
		
		String directory="C:\\";
		proced(directory);

		
	}
	
	private static void proced(String dir){
		File f=new File(dir);
		if(!f.exists()) System.out.println("Not found directory "+f.toString());
		else{
			if(f.isDirectory()){
				System.out.print(dir+">");
				Scanner input=new Scanner(System.in);
				String command;
				command=input.nextLine();
				dir=parser(command,dir);
				proced(dir);
			}
			else{
				System.out.println("Error. It is not directory "+f.toString());
			}
		}
		}	
	
		

		
public static String parser(String command, String dir){
		
		String[] arrayCommand=command.split(" ");
					
 try{		
		switch (Command.myCommand.valueOf(arrayCommand[0])) {
                   		
        case dir:   System.out.print(Dir.printDir(dir)); break;
        case mkdir: Dir.mkdir(arrayCommand[1]);break;
        case move: Dir.move(arrayCommand[1],arrayCommand[2]);break;
        case copy: Dir.copy(arrayCommand[1],arrayCommand[2]); break;
        case find:  System.out.print(Dir.searchInTree(dir,arrayCommand[1]));break;
        case tree:  Dir.DirToTree(arrayCommand[1],"");break;
        				
        default:    System.out.println("Unknown command.");	break;
    }
}
		catch(IllegalArgumentException e){
			System.out.println("Unknown command.");
		}	
	return dir;
  }

public static class Command {
	public enum Commands {
	    dir,copy,move,mkdir,tree,find
	}
	public static Commands myCommand;  
  }



public static class Dir{
	
	
public static String printDir(String dir){
	String pS="";
	        File f=new File(dir);
	        if(!f.exists()){
	        	System.out.println("Not found directory "+f.toString());
	        }
	        else{
	        	pS+=">>> "+dir+"\n";
	            String[] dirList=f.list();
	            int k=0;
	            String[] fileList=new String[dirList.length];
	            for(int i=0;i<dirList.length;i++){
	                    File f1=new File(dir+File.separator+dirList[i]);
	                    if(!f1.isFile()) pS+=dirList[i]+"\n";
	                    else{
	                            fileList[k]=dirList[i];
	                            k++;
	                    }
	            }
	            for(int i=0;i<k;i++){
	            	pS+=fileList[i]+"\n";
	            }
	        }
	        return pS;
	    }	

public static boolean mkdir(String sNewDir){
	File newD=new File(sNewDir);
	
	if(newD.mkdirs())  {
		System.out.println("create");
		return true;
		}
	else{				
		System.out.println("Error. Directory doesn't create.");
		return false;
	}
}

public static boolean copy(String sFileFrom, String sFileTo){
	
File fileFrom=new File(sFileFrom);
if(!fileFrom.exists()) {
	System.out.println("Not found file "+fileFrom.toString());
    return false;
	}
 else{
	try{
	File fileTo=new File(sFileTo);
	if(fileTo.exists()){		
		System.out.println("file there");return false;}
			else{
			InputStream in = new FileInputStream(fileFrom);
			OutputStream out = new FileOutputStream(fileTo);
			  byte[] b = new byte[1024];
			  int l;
			  while ((l = in.read(b)) > 0){
			  out.write(b,0,l);
			  }
			  in.close();
			  out.close();
			  System.out.println("File copied.");
			  return true;
			}
			}	
			catch(IOException e){
				  System.out.println(e.getMessage());
				  return false;
			}
	}
	
} 

public static boolean move(String fileFrom, String fileTo){
	if(copy(fileFrom,fileTo)){
		if(del(fileFrom)) return true; else return false;
	}
	else{
		return false;
	}
}

public static boolean del(String dir){
	File dele=new File(dir);
	if(dele.delete()) {
		return true;
	}
	else{ 
		System.out.println("Error");
			return false;
	}

}


public static void DirToTree(String pathDir, String level)
{
        File dir = new File(pathDir);
        String[] pathList= dir.list();
        File fileDirTemp=null;
        level+=" ";
        for (String pathTemp: pathList)
        {
                fileDirTemp= new File(pathDir+'/'+pathTemp);
                if (fileDirTemp.isDirectory())
                {
                        System.out.println(level+"-"+pathTemp+'/');
                        DirToTree(pathDir+'/'+pathTemp,level+ "   ");
                } else {
                        System.out.println(level+"-"+pathTemp);
                }
        }       
}

private static String printString;

public static String searchInTree(String dir, String mask){
	printString="";
	printString=searchInTreeRecursion(dir, mask);
	return printString;
}


private static String searchInTreeRecursion(String dir, String mask){
	File f=new File(dir);
	if(!f.exists()) ;
	else
	{

	searchInDir(dir,mask);
	String[] dirList=f.list();
	for(int i=0;i<dirList.length;i++){
		String absDir=dir+File.separator+dirList[i];
		File f1=new File(absDir);
		if(!f1.isFile()) searchInTreeRecursion(absDir,mask);
		}
	}
	return printString;
}

public static void searchInDir(String dir, String mask){
	File f=new File(dir);
	if(!f.exists()) System.out.println("Not found directory "+f.toString());
	else
	{
	
	
	String[] dirList=f.list();
	String[] arrayMask;
	
	if(mask.contains("*")) mask=mask.replace("*", "`");
	
	if(mask.contains("`")){
			arrayMask = mask.split("`");
	
	for(int i=0;i<dirList.length;i++) {
		String sDirList=dirList[i];
		int k=0;
		while(sDirList.length()>0 && k<arrayMask.length){
			if(sDirList.startsWith(arrayMask[k])){
				for(int r=0; r<arrayMask[k].length();r++) sDirList=cutDir(sDirList);
				k++;
			}
			else{ 
				sDirList=cutDir(sDirList);
			}
		}
		if(k>=arrayMask.length)	printName(dir+File.separator+dirList[i]);
	}
	
	}
	else{
		for(int i=0;i<dirList.length;i++) {
			if(dirList[i].equals(mask))	printName(dir+File.separator+dirList[i]);
		}
	}
	
	}
}



private static String cutDir(String dir){
	int ld=dir.length();
	char[] newDir=new char[ld-1];
	dir.getChars(1, ld, newDir, 0);
	String sNewDir=newDir.toString();
	sNewDir=sNewDir.copyValueOf(newDir);
return sNewDir;
}

private static void printName(String dirName){
	File f1=new File(dirName);
	Date date=new Date(f1.lastModified());
	printString+=f1.getName()+" \t"+date+" "+f1.getPath()+"\n";
}




}
}


